#include "particle.h"
#include <math.h>


CParticle::CParticle(){

}
void CParticle::Init(	Vector2D pos, Vector2D vel,
					 Color color, float size, float ttl){	
						 _pos = pos;
						 _vel = vel;
						 _color = color;
						 _size = size;
						 _ttl = ttl;
						 _isActive = true;

						 _angle = 0.1 + (float)(rand()%20)/200.0;       
						 _angularVelocity = (float)(rand()%20)/200.0;
						 _anpha = 20 + rand()%230;
}

CParticle::~CParticle(){

}

void CParticle::Update()
{
	_ttl--;
	if(_ttl <= 0){
		_isActive = false;
		return;
	}

	_pos.x += _vel.x;
	_pos.y += _vel.y;
	_angle += _angularVelocity;

	if(_anpha > 5)
		_anpha--;
}

CParticleEngine::CParticleEngine(){
}

CParticleEngine::CParticleEngine(LPD3DXSPRITE spriteHandler, Vector2D pos){
	_SpriteHandler = spriteHandler;
	_emitterPos = pos;
	_isActive = true;
	_time = 0;
	_lastTime = GetTickCount();
}

CParticleEngine::~CParticleEngine(){
	_Image->Release();	
}

void CParticleEngine::Init(LPDIRECT3DTEXTURE9 image){
	_Image=image;
}

float del = 0.1;

void CParticleEngine::Update(int delta){
	int count = 0;
	int i;
	Vector2D pos;

	for(i = 0; i < _particles.size(); i++){
		if(_particles[i].IsAlive())
			_particles[i].Update();						
		else
			RemoveAt(i);
	}
}

void CParticleEngine::Render(int vituralLeft)
{	
	D3DXMATRIX matrixOld;
	D3DXMATRIX matrixNew;
	D3DXVECTOR2 scaling(1.0,1.0);
	D3DXVECTOR2 trans=D3DXVECTOR2(0.0,0.0);
	D3DXVECTOR3 position(0.0,0.0, 0.0);		
	_virtualPosX=vituralLeft;
	_SpriteHandler->Begin(D3DXSPRITE_ALPHABLEND);
	for(int i = 0; i < _particles.size(); i++){
		if(_particles[i].IsAlive()){
			_SpriteHandler->GetTransform(&matrixOld);			

			D3DXVECTOR2 spriteCentre=D3DXVECTOR2((float)(_particles.at(i)._pos.x + 8),
				(float)(_particles.at(i)._pos.y + 8));
			D3DXMatrixTransformation2D(&matrixNew, NULL, 0.0, &scaling, &spriteCentre, _particles.at(i)._angle, &trans);
			position.x = _particles.at(i)._pos.x + (8+rand()%2);
			position.y = _particles.at(i)._pos.y + 8;	
			_SpriteHandler->SetTransform(&matrixNew);
			_SpriteHandler->Draw(_Image,NULL,NULL,&position, D3DCOLOR_ARGB(_particles.at(i)._anpha,200,200,255));
			_SpriteHandler->SetTransform(&matrixOld);
		}
	}
	_SpriteHandler->End();
}


void CParticleEngine::RemoveAt(int index){
	if(index<0 || index>=_particles.size())
		return;
	_particles.erase(_particles.begin()+index);
}

void CParticleEngine::GenarateNewParticle(int x, int y, int numPar)
{
	Vector2D vel;	
	Color color;
	Vector2D parPos;
	x=x-_virtualPosX;
	for(int i = 0; i < numPar; i++){
		CParticle par;

		vel.x =  ((((rand()) % 80)+20) * (rand()%2==0?1:-1)  ) / 50.0;
		vel.y =  ((rand() % 80)+20) / 20.0;

		color.r =  rand() % 105;
		color.g =  rand() % 255;
 		color.b =  rand() % 255;

		parPos.x = x + (rand()%10)* (rand()%2 == 0 ? 1 : -1);
		parPos.y = y- 20  + (rand()%10);

		par.Init(parPos, vel, color, 200, 15 + rand()%15);
		Add(par);	
	}
}